/*
 main.cpp
 article-01-skeleton

 Copyright 2012 Thomas Dalling - http://tomdalling.com/

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#include "z3d/zel_engine.h"
#include "Helper.h"


// Put the three triangle verticies into the VBO
GLfloat vertexData[] = {
    //  X     Y     Z
     0.0f, 0.8f, 0.0f,
    -0.8f,-0.8f, 0.0f,
     0.8f,-0.8f, 0.0f,
};

// globals
z3d::Program* gProgram = NULL;
z3d::GraphicObject graphicObject;
z3d::Z3D_Mesh* mesh = new z3d::Z3D_Mesh("test");

/*
// returns the full path to the file `fileName` in the same folder as the executable
static std::string ResourcePath(std::string fileName) {
	return GetProcessPath() + fileName;
}
*/
// loads the vertex shader and fragment shader, and links them to make the global gProgram
static void LoadShaders() {
    std::vector<z3d::ShaderItem> shaders;
    shaders.push_back(z3d::ShaderItem("vertex_shader_001","../resources/programs/shaders/vertex-shader.txt", GL_VERTEX_SHADER));
    shaders.push_back(z3d::ShaderItem("fragment_shader_001","../resources/programs/shaders/fragment-shader.txt", GL_FRAGMENT_SHADER));
    gProgram = new z3d::Program(shaders);
}


// loads a triangle into the VAO and VBO globals: gVAO and gVBO
static void LoadTriangle() {  
    // unbind the VBO and VAO
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}
 

// draws a single frame
static void Render() {
    // clear everything
    glClearColor(0, 0, 0, 1); // black
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    graphicObject.display();
    
    // swap the display buffers (displays what was just drawn)
    glfwSwapBuffers();
}


// the program starts here
int main(int argc, char *argv[]) {
#ifdef _WIN32
    std::string configFile("../../resources/programs/simple_window.ini");
#else
	std::string configFile("../resources/programs/simple_window.ini");
#endif
    


    z3d::Engine engine;
    if(!engine.init(configFile))
        return EXIT_FAILURE;

    // load vertex and fragment shaders into opengl
    LoadShaders();

    mesh->setVertices(std::vector<float>(vertexData, vertexData + 9));
    mesh->loadTriangles();
    graphicObject.setMesh(mesh);
    graphicObject.setProgram(gProgram);
    graphicObject.bindProgramToMesh();
    // create buffer and fill it with the points of the triangle
    //LoadTriangle();

    // run while the window is open
    while(engine.isRunning()){
        // draw one frame
        Render();
    }

    // clean up and exit
    engine.shutDown();
    return EXIT_SUCCESS;
}
